﻿using System;

namespace BinarySearchTree
{
    public class TreeNode<T>
        where T : IComparable<T>
    {
        public bool HasValue { get; set; }
        private T nodeValue;

        public TreeNode<T> LeftNode { get; set; }
        public TreeNode<T> RightNode { get; set; }
        public T Value
        {
            get
            {
                if (HasValue)
                {
                    return nodeValue;
                }
                else
                {
                    throw new ArgumentNullException();
                }
            }
            set
            {
                if (!HasValue)
                {
                    LeftNode = new TreeNode<T>();
                    RightNode = new TreeNode<T>();
                    HasValue = true;
                }
                nodeValue = value;
            }
        }

        public TreeNode()
        {
            this.HasValue = false;
            this.LeftNode = null;
            this.RightNode = null;
        }

        public TreeNode(T value)
        {
            this.HasValue = true;
            this.Value = value;
            this.LeftNode = new TreeNode<T>();
            this.RightNode = new TreeNode<T>();
        }
    }
}
